package ils.domain.issue.model;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class IssueRepositoryImpl implements IssueRepository
{
    private static final String GET_BY_BARCODE = "select issue from Issue as issue where issue.item.barcode = ? and issue.returnDate is null";
    private static final String GET_OPEN_BY_USER = "select issue from Issue as issue where issue.userId = ? and issue.returnDate is null";
    private static final String GET_OPEN = "select issue from Issue as issue where issue.returnDate is null";
    private SessionFactory sessionFactory;

    @Override
    public Issue createIssue(Issue issue)
    {
        Session session = sessionFactory.getCurrentSession();
        session.save(issue);
        return issue;
    }

    @Override
    public Issue getIssue(String barcode)
    {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(GET_BY_BARCODE);
        query.setString(0, barcode);
        Iterator iterator = query.iterate();
        if(iterator.hasNext()) return (Issue)iterator.next();
        else return null;
    }

    @Override
    public Issue getIssue(int id) {
        Session session = sessionFactory.getCurrentSession();
        return (Issue)session.get(Issue.class, id);
    }

    @Override
    public List<Issue> getOpenIssuesForUser(int userId)
    {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(GET_OPEN_BY_USER);
        query.setInteger(0, userId);
        return query.list();
    }

    @Override
    public List<Issue> getIssuesForABook(int bookId, int firstResult, int count) {
        Session session = sessionFactory.getCurrentSession();
        String hql = "select issue from Issue as issue where issue.item.bookId = ? order by issue.issueDate desc";
        Query query = session.createQuery(hql);
        query.setInteger(0, bookId);
        query.setFirstResult(firstResult);
        query.setMaxResults(count);
        return query.list();
    }

    @Override
    public List<Issue> getOpenIssues(int firstResult, int maxResult)
    {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(GET_OPEN);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResult);
        return query.list();
    }

    @Override
    public List<Issue> getOpenIssuesForBranch(int branchId) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Issue> searchForIssues(IssueCriteria issueCriteria) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Issue editIssue(Issue issue) {
        Session session = sessionFactory.getCurrentSession();
        session.save(issue);
        return issue;
    }

    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }



}
